from sqlalchemy import Column, Integer, String, DateTime, Text, Enum
from datetime import datetime
import enum
from .BaseModel import BaseModel

class OperationType(enum.Enum):
    LOGIN = "login"
    LOGOUT = "logout"

class LoginRecord(BaseModel):
    __tablename__ = 'login_records'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    phone = Column(String(20), nullable=False, comment='手机号')
    operation_time = Column(DateTime, default=datetime.now, comment='操作时间')
    ip_address = Column(String(45), comment='IP地址')
    user_agent = Column(Text, comment='用户代理')
    status = Column(Enum(OperationType), nullable=False, comment='操作类型')
    
    def __repr__(self):
        return f"<LoginRecord(id={self.id}, phone='{self.phone}', status='{self.status.value}')>"
    
    def to_dict(self):
        return {
            'id': self.id,
            'phone': self.phone,
            'operation_time': self.operation_time.isoformat() if self.operation_time else None,
            'ip_address': self.ip_address,
            'user_agent': self.user_agent,
            'status': self.status.value if self.status else None
        }
